{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "import imagededup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "single_image_path = Path('../tests/data/mixed_images/ukbench00120.jpg')\n",
    "image_dir = Path('../tests/data/mixed_images')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Hashing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Hash of a single image using Perceptual hashing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from imagededup.methods import PHash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "phasher = PHash()\n",
    "phash_string = phasher.encode_image(image_file = single_image_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'9fee256239984d71'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phash_string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Hash of a single image using average hashing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'81b8bc3c3c3c1e0a'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from imagededup.methods import AHash\n",
    "ahasher = AHash()\n",
    "ahash_string = ahasher.encode_image(single_image_path)\n",
    "ahash_string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Hash all images in a directory using Difference hashing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start: Calculating hashes...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/6 [00:00<?, ?it/s]2019-10-01 19:00:14,689: WARNING Invalid image file /Users/tanuj.jain/Documents/image-dedup/examples/../tests/data/mixed_images/ukbench09268_corrupt.jpg:\n",
      "cannot identify image file PosixPath('/Users/tanuj.jain/Documents/image-dedup/examples/../tests/data/mixed_images/ukbench09268_corrupt.jpg')\n",
      "100%|██████████| 6/6 [00:00<00:00, 76.51it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "End: Calculating hashes!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from imagededup.methods import DHash\n",
    "\n",
    "dhasher = DHash()\n",
    "encodings = dhasher.encode_images(image_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ukbench00120_resize.jpg': '2b69707551f1b87a',\n",
       " 'ukbench00120_rotation.jpg': '74d2d1717168acd0',\n",
       " 'ukbench00120.jpg': '2b69707551f1b87a',\n",
       " 'ukbench00120_hflip.jpg': '2b69f1517570e2a1',\n",
       " 'ukbench09268.jpg': 'ac9c72f8e1c2c448'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "encodings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CNN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from imagededup.methods import CNN"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Single image CNN encoding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /Users/tanuj.jain/anaconda3/envs/dedup_package_env/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2019-10-01 19:00:19,680: INFO Initialized: MobileNet pretrained on ImageNet dataset sliced at last conv layer and added GlobalAveragePooling\n"
     ]
    }
   ],
   "source": [
    "cnn_encoder = CNN()\n",
    "cnn_encoding = cnn_encoder.encode_image(single_image_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.5847789 , 0.17494635, 0.        , ..., 1.0568997 , 0.00498394,\n",
       "        0.6473189 ]], dtype=float32)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cnn_encoding"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Encodings for all images in a directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2019-10-01 19:00:23,740: INFO Initialized: MobileNet pretrained on ImageNet dataset sliced at last conv layer and added GlobalAveragePooling\n",
      "2019-10-01 19:00:23,741: INFO Start: Image encoding generation\n",
      "2019-10-01 19:00:23,877: WARNING Invalid image file /Users/tanuj.jain/Documents/image-dedup/examples/../tests/data/mixed_images/ukbench09268_corrupt.jpg:\n",
      "cannot identify image file PosixPath('/Users/tanuj.jain/Documents/image-dedup/examples/../tests/data/mixed_images/ukbench09268_corrupt.jpg')\n",
      "2019-10-01 19:00:24,050: WARNING Invalid image file /Users/tanuj.jain/Documents/image-dedup/examples/../tests/data/mixed_images/ukbench09268_corrupt.jpg:\n",
      "cannot identify image file PosixPath('/Users/tanuj.jain/Documents/image-dedup/examples/../tests/data/mixed_images/ukbench09268_corrupt.jpg')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r",
      "1/1 [==============================] - 1s 720ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2019-10-01 19:00:24,496: INFO End: Image encoding generation\n"
     ]
    }
   ],
   "source": [
    "cnn_encoder = CNN()\n",
    "cnn_encodings = cnn_encoder.encode_images(image_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ukbench00120.jpg': array([1.5847782 , 0.1749464 , 0.        , ..., 1.0568993 , 0.00498396,\n",
       "        0.6473194 ], dtype=float32),\n",
       " 'ukbench00120_hflip.jpg': array([1.8885472 , 0.2520814 , 0.06678697, ..., 0.89278036, 0.02286635,\n",
       "        0.91840214], dtype=float32),\n",
       " 'ukbench00120_resize.jpg': array([1.707229  , 0.19885841, 0.0129852 , ..., 1.4586834 , 0.00776921,\n",
       "        0.83636326], dtype=float32),\n",
       " 'ukbench00120_rotation.jpg': array([0.5142981 , 0.20380044, 0.        , ..., 0.02884004, 0.00844299,\n",
       "        0.9559768 ], dtype=float32),\n",
       " 'ukbench09268.jpg': array([0.4290448 , 0.24964048, 0.50022906, ..., 0.8208346 , 0.16613719,\n",
       "        0.6475953 ], dtype=float32)}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cnn_encodings"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
